/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dtn.getdata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.net.Uri;

import dtn.supports.Constant;

/**
 * 
 * @author asus
 */
public class AndroidGetData {

	static int max_content = 30;
	static int timeout = 0;

	// /**
	// * @param args the command line arguments
	// */
	// public static void main(String[] args) throws IOException {
	// List<NewsPreview> listNews =
	// AndroidDataFactory.getListNewsPreview(AndroidDataFactory.VIEC_LAM, 1);
	// for (NewsPreview news : listNews) {
	// System.out.println(news.toString());
	// }
	// }

	/**
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static List<NewsPreview> getListNewNewsPreview(String url)
			throws IOException {
		List<NewsPreview> listNews; // list result
		Document doc; // html document from url
		Elements elements; // elements of document have the same article
		int count; // number of valid element from document

		// create result list
		listNews = new ArrayList<NewsPreview>();

		// get html document from url
		doc = Jsoup.connect(url).timeout(timeout).get();

		// init number of valid elements
		count = 0;

		// select element content newnews
		elements = doc.select(ConstantCompare.NewsPreview_ID_Panel_NewNews);
		for (Element element : elements) {
			String[] temp = element.attr(
					ConstantCompare.NewsPreview_ID_Panel_NewNews_Attribute)
					.split("'");
			try {
				NewsPreview news = new NewsPreview();

				news.setImageUrl(temp[1]);
				news.setTitle(temp[3]);
				news.setHotContent(temp[5]);
				news.setUrl(element.attr("href"));
				listNews.add(news);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return listNews;
	}

	/**
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static String getDetailHTMLContent(String url) throws IOException {
		Document doc;
		Elements elements;
		Element element;
		String res;

		doc = Jsoup.connect(url).timeout(timeout).get();

		elements = doc.select(ConstantCompare.Detail_Content);
		element = elements.first();

		res = element.toString();
		res = res.replaceAll("../data",
				"http://www.thanhdoan.hochiminhcity.gov.vn/webtd/data");

		res = "<html><body>" + res + "</body></html>";

		return res;
	}

	/**
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static List<NewsPreview> getListNewsPreview(String url,
			boolean isHead) throws IOException {
		List<NewsPreview> listNews; // list result
		Document doc; // html document from url
		Elements elements; // elements of document have the same article
		int count; // number of valid element from document

		// create result list
		listNews = new ArrayList<NewsPreview>();

		// get html document from url
		doc = Jsoup.connect(url).timeout(timeout).get();
		Constant.testValue = doc.toString();

		// init number of valid elements
		count = 0;

		// select content panel element from url's document
		elements = doc.select(ConstantCompare.NewsPreview_ID_Panel_All);

		// get content of each news
		Element element = elements.first();

		// get hosted news
		if (isHead) {
			elements = element
					.select(ConstantCompare.NewsPreview_ID_Panel_Hosted);
			for (Element ele : elements) {
				listNews.add(getNewsPreviewFromElement(ele,
						ConstantCompare.NewsPreview_ID_Panel_Hosted_Title,
						ConstantCompare.NewsPreview_ID_Panel_Hosted_Image,
						ConstantCompare.NewsPreview_ID_Panel_Hosted_Hot));
				count++;
			}
		}

		// get normal news
		elements = element.select(ConstantCompare.NewsPreview_ID_Panel_One);
		// check each news for full content: title, url, imageurl, hotcontent
		for (Element ele : elements) {
			listNews.add(getNewsPreviewFromElement(ele,
					ConstantCompare.NewsPreview_ID_Panel_One_Title,
					ConstantCompare.NewsPreview_ID_Panel_One_Image,
					ConstantCompare.NewsPreview_ID_Panel_One_Hot));
			count++;
		}

		return listNews;
	}

	/**
	 * 
	 * @param element
	 *            html element to analyze
	 * @param cmpTitle
	 *            string to compare title
	 * @param cmpImage
	 *            string to compare image
	 * @param cmpHot
	 *            string to compare hot content
	 * @return
	 */
	private static NewsPreview getNewsPreviewFromElement(Element element,
			String cmpTitle, String cmpImage, String cmpHot) {
		// res value
		NewsPreview news = new NewsPreview();
		Elements temp = element.select(cmpTitle);
		for (Element e : temp) {
			// get url of full content
			news.setUrl(e.attr("href"));
			// get title content
			news.setTitle(e.text());
		}
		// get image
		temp = element.select(cmpImage);
		for (Element e : temp) {
			String urlStr = e.attr("src");
			news.setImageUrl(urlStr);
		}
		// get hot content
		temp = element.select(cmpHot);
		for (Element e : temp) {
			news.setHotContent(e.text());
		}

		return news;
	}
}
